package com.example.demo.generator.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.demo.generator.entiy.TMenu;
import com.example.demo.generator.entiy.TMenuRole;
import com.example.demo.generator.entiy.TRole;
import com.example.demo.generator.service.TMenuRoleService;
import com.example.demo.generator.service.TRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "角色接口")
@CrossOrigin("*")
public class RoleController {

    @Autowired
    public TRoleService tRoleService;

    @Autowired
    public TMenuRoleService tMenuRoleService;

    @ApiOperation("获取角色列表")
    @GetMapping("getrolelist")
    //获取角色列表
    public R getrolelist(@RequestParam(value = "pageNum", defaultValue = "") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "") Integer pageSize, @RequestParam(value = "name", defaultValue = "") String name) {
        if (pageNum !=null && pageSize != null){
            if (name != null && !name.equals("")) {
                QueryWrapper<TRole> roleQueryWrapper = new QueryWrapper<>();
                roleQueryWrapper.eq("enabled", 1);
                roleQueryWrapper.like("rolename", name);
                PageHelper.startPage(pageNum, pageSize);
                List<TRole> tRoleList = tRoleService.list(roleQueryWrapper);
                PageInfo<TRole> pageInfo = new PageInfo<>(tRoleList);
                return R.ok(pageInfo);
            } else {
                QueryWrapper<TRole> roleQueryWrapper = new QueryWrapper<>();
                roleQueryWrapper.eq("enabled", 1);
                PageHelper.startPage(pageNum, pageSize);
                List<TRole> tRoleList = tRoleService.list(roleQueryWrapper);
                PageInfo<TRole> pageInfo = new PageInfo<>(tRoleList);
                return R.ok(pageInfo);
            }
        }else{
            QueryWrapper<TRole>roleQueryWrapper=new QueryWrapper<>();
            roleQueryWrapper.eq("enabled",1);
            List<TRole> tRoleList = tRoleService.list(roleQueryWrapper);
            return R.ok(tRoleList);
        }
    }

    @ApiOperation("添加角色")
    @PostMapping("insertrole")
    public R insertrole(@RequestBody TRole role){
        try{
            tRoleService.save(role);
            return R.ok(role);
        }catch (Exception ex){
            return R.failed(ex.toString());
        }
    }

    @ApiOperation("编辑角色")
    @PostMapping("editrole")
    public R editrole(@RequestBody TRole role){
        try{
            tRoleService.updateById(role);
            TRole tRole=tRoleService.getById(role.getId());
            return R.ok(tRole);
        }catch (Exception ex){
            return R.failed(ex.toString());
        }
    }

    @ApiOperation("删除角色")
    @GetMapping("deleterole")
    public R deleterole(@RequestParam("id")Integer id){
        try{
            TRole tRole=tRoleService.getById(id);
            tRole.setEnabled(0);
            tRoleService.updateById(tRole);
            return R.ok("success");
        }catch (Exception ex){
            return R.failed(ex.toString());
        }
    }

    @GetMapping("getrolemenu")
    @ApiOperation("获取角色菜单权限")
    //角色权限
    public R getrolemenu(@RequestParam("id")Integer id){
        List<TMenu>tMenuList=tRoleService.getrolemenu(id);
        return R.ok(tMenuList);
    }

    @PostMapping("editrolemenu")
    @ApiOperation("编辑角色权限")
    public R editrolemenu(@RequestBody Map<String,String> map){
        String s=map.get("mids");
        //解析菜单权限分组
        ArrayList<String>menuArray=new ArrayList<>();
        if(map.get("mids").contains(",")) {
            menuArray = new ArrayList<>(Arrays.asList(map.get("mids").split(","))) ;
        }else{
            menuArray.add(map.get("mids"));
        }
        //清除权限，重新添加
        //清除权限
        QueryWrapper<TMenuRole>tMenuRoleQueryWrapper=new QueryWrapper<>();
        tMenuRoleQueryWrapper.eq("rid",map.get("rid"));
        try {
            if (tMenuRoleService.remove(tMenuRoleQueryWrapper)) {
                //重新添加
                for (int i = 0; i < menuArray.size(); i++) {
                    TMenuRole tMenuRole = new TMenuRole();
                    tMenuRole.setRid(Integer.parseInt(map.get("rid")));
                    tMenuRole.setMid(Integer.parseInt(menuArray.get(i)));
                    tMenuRoleService.save(tMenuRole);
                }
                return R.ok("success");
            }else{
                return R.failed("error");
            }

        }catch (Exception ex){
            return R.failed(ex.toString());
        }

    }
}
